﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db;
using HengTong.Model.Db.System;


namespace HengTong.ViewModel.Wechat.SysWechatUserVms
{
    public partial class SysWechatUserListVm : BasePagedListVM<SysWechatUserView, SysWechatUserSearcher>
    {
        protected override IEnumerable<IGridColumn<SysWechatUserView>> InitGridHeader()
        {
            return new List<GridColumn<SysWechatUserView>>
            {
                this.MakeGridHeader(x => x.WebOpenId),
                this.MakeGridHeader(x => x.MpOpenId),
                this.MakeGridHeader(x => x.MiniProgramOpenId),
                this.MakeGridHeader(x => x.UnionId),
                this.MakeGridHeader(x => x.NickName),
                this.MakeGridHeader(x => x.HeadImgUrl),
                this.MakeGridHeader(x => x.UserName),
                this.MakeGridHeader(x => x.ITCode),
                this.MakeGridHeader(x => x.CellPhone),
                this.MakeGridHeader(x => x.GroupName),
                this.MakeGridHeaderAction(width: 200)
            };
        }

        public override IOrderedQueryable<SysWechatUserView> GetSearchQuery()
        {
            var query = DC.Set<SysWechatUser>()
                .CheckContain(Searcher.WebOpenId, x => x.WebOpenId)
                .CheckContain(Searcher.MpOpenId, x => x.MpOpenId)
                .CheckContain(Searcher.MiniProgramOpenId, x => x.MiniProgramOpenId)
                .CheckContain(Searcher.UnionId, x => x.UnionId)
                .CheckContain(Searcher.NickName, x => x.NickName)
                .CheckEqual(Searcher.UserId, x => x.UserId)
                .CheckContain(Searcher.ITCode, x => x.User.ITCode)
                .CheckContain(Searcher.UserName, x => x.User.Name)
                .CheckContain(Searcher.CellPhone, x => x.User.CellPhone)
                .CheckContain(Searcher.GroupName, x => x.User.Group.GroupName)
                .Select(x => new SysWechatUserView
                {
                    ID = x.ID,
                    WebOpenId = x.WebOpenId,
                    MpOpenId = x.MpOpenId,
                    MiniProgramOpenId = x.MiniProgramOpenId,
                    UnionId = x.UnionId,
                    NickName = x.NickName,
                    HeadImgUrl = x.HeadImgUrl,
                    UserName = x.User.Name,
                    ITCode = x.User.ITCode,
                    CellPhone = x.User.CellPhone,
                    GroupName = x.User.Group.GroupName,
                })
                .OrderBy(x => x.ID);
            return query;
        }
    }

    public class SysWechatUserView : SysWechatUser
    {
        [Display(Name = "姓名")]
        public String UserName { get; set; }
        [Display(Name = "工号")]
        public string ITCode { get; set; }
        [Display(Name = "手机号")]
        public string CellPhone { get; set; }
        [Display(Name = "部门")]
        public string GroupName { get; set; }
    }
}